package Controllers;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class TagsManageController extends Application implements Initializable {

    @FXML
    private CheckBox tag1;
    @FXML
    private CheckBox tag2;
    @FXML
    private CheckBox tag3;
    @FXML
    private CheckBox tag4;
    @FXML
    private CheckBox tag5;
    @FXML
    private CheckBox tag6;
    @FXML
    private CheckBox tag7;
    @FXML
    private CheckBox tag8;
    @FXML
    private Button confirm;
    @FXML
    private AnchorPane Tags_Fxml;

    private static final String[] tags = {"学习","日常","游戏","运动","科技","艺术","影视","杂谈"};
    private boolean[] checked = new boolean[8];//装八个标签是否被选中

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            AnchorPane root = FXMLLoader.load(getClass().getResource("../Resources/TagsManagement.fxml"));
            Scene scene = new Scene(root, 288.0, 300.0);
            //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.getIcons().add(new Image("/Images/logoblue.png"));
            primaryStage.setTitle("选择标签");
            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void confirm(){
        int selectedTagNum = 0;
        checked[0] = tag1.isSelected();
        checked[1] = tag2.isSelected();
        checked[2] = tag3.isSelected();
        checked[3] = tag4.isSelected();
        checked[4] = tag5.isSelected();
        checked[5] = tag6.isSelected();
        checked[6] = tag7.isSelected();
        checked[7] = tag8.isSelected();
        for(int i=0;i<checked.length;i++){
            if(checked[i]){
                selectedTagNum++;
            }
        }
        if(selectedTagNum>4){
            String info = "只能选择四个标签！请重新选择!";
            Alert alert = new Alert(Alert.AlertType.INFORMATION, info,
                    new ButtonType("确定", ButtonBar.ButtonData.YES));
            alert.setHeaderText(null);
            alert.setTitle("提示");
            alert.show();
        }else if(selectedTagNum == 4){
            writeTags();//写入当前tags
            String info = "设置标签成功！";
            Alert alert = new Alert(Alert.AlertType.INFORMATION, info,
                    new ButtonType("确定", ButtonBar.ButtonData.YES));
            alert.setHeaderText(null);
            alert.setTitle("提示");
            alert.show();
            Stage stage = (Stage) Tags_Fxml.getScene().getWindow();
            stage.close();
        }else{
            String info = "不能少于四个标签！请重新选择！";
            Alert alert = new Alert(Alert.AlertType.INFORMATION, info,
                    new ButtonType("确定", ButtonBar.ButtonData.YES));
            alert.setHeaderText(null);
            alert.setTitle("提示");
            alert.show();
        }
    }
    public void writeTags(){//写入文件
        List<String> selectedTags = new ArrayList<>();
        for(int i=0;i<checked.length;i++){
            if(checked[i]){
                selectedTags.add(tags[i]);
            }
        }
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("src/UserPosts/currentTags.txt"));
            for(String tag : selectedTags){
                writer.write(tag+"\r\n");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
